﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Nyamr
{
    public class Plan
    {
        public class InvalidDataException : Exception 
        {
            public InvalidDataException(string message) : base(message) { }
        }
        
        public Plan() 
        {
            Task = "path to dll with task";
            TaskName = "task name";
            Threads = 1;
            Source = "path to input data directory";
            Target = "path to output data directory";
            Settings = "task settings";
        }
        
        public string Task { get; set; }
        public string TaskName { get; set; }
        public int Threads { get; set; }
        public string Source { get; set; }
        public string Target { get; set; }
        public string Settings { get; set; }

        public void Save(string path) 
        {
            var file = new FileStream(path, FileMode.CreateNew);
            var serializer = new XmlSerializer(typeof(Plan));
            serializer.Serialize(file, this);
            file.Flush();
            file.Close();
        }

        public static Plan Load(string path)
        {
            var file = new FileStream(path, FileMode.Open);
            var serializer = new XmlSerializer(typeof(Plan));
            var plan = (Plan)serializer.Deserialize(file);
            file.Close();
            return plan;
        }

        public void Validate() 
        {
            if (!File.Exists(Task)) 
                throw new InvalidDataException(string.Format("Assembly '{0}' should exist.", Task));
            if (!Directory.Exists(Source))
                throw new InvalidDataException(string.Format("Directory '{0}' should exist.", Source));
            if (Directory.Exists(Target))
                throw new InvalidDataException(string.Format("Directory '{0}' shouldn't exist.", Target));
            if (File.Exists(Target))
                throw new InvalidDataException(string.Format("File '{0}' shouldn't exist.", Target));
        }
    }
}
